#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :test.py
# @Time      :2023/7/21 
# @Author    :CL
# @email     :1037654919@qq.com

import  os
import requests
proxies = {'http': '127.0.0.1:15732',
           'https': '127.0.0.1:15732'}
headers = {
    "authority": "pixabay.com",
    "accept": "application/json",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "referer": "https://pixabay.com/zh/images/search/%e6%b1%bd%e8%bd%a6/?pagi=2",
    "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Microsoft Edge\";v=\"114\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58",
    "x-csrftoken": "F9W96tWQfRMDkP2ZlrtfLnqhjMj1DwO7uPyfciJ030TOoDJ4Ci0KRKMKpaCKiqex",
    "x-fetch-bootstrap": "1"
}
cookies = {
    "csrftoken": "F9W96tWQfRMDkP2ZlrtfLnqhjMj1DwO7uPyfciJ030TOoDJ4Ci0KRKMKpaCKiqex",
    "lang": "zh",
    "anonymous_user_id": "a3853ac95d15426e8edc47e8a2c0ee94",
    "__cf_bm": "lrZ90n1ZCsYt3xo7DtCNdeP5.AVzptpVFc8LQFmR3P8-1689927766-0-AR2bJs8ibaa0J7WCapUa6EPvDjs9kGrWb3PSO9hw0faYzIfBScJZLQbNzGR0XCmqVjmhfz3xt+xMv1L8UUV5ck4=",
    "dwf_show_canva_banner_ads": "True",
    "dwf_strict_media_search": "False",
    "is_human": "1",
    "_sp_ses.aded": "*",
    "OptanonConsent": "isGpcEnabled=0&datestamp=Fri+Jul+21+2023+16%3A23%3A23+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.31.0&isIABGlobal=false&hosts=&consentId=603fee4b-7b90-4ba9-b877-0e9eef09835a&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A0%2CC0003%3A0%2CC0004%3A0&AwaitingReconsent=false",
    "_sp_id.aded": "8fb0be41-4d23-419a-a783-4a9b5591f7f2.1689927770.1.1689927804.1689927770.f1b3a17c-e599-4ecb-9883-7624b22b4662"
}
proxies = {'http': '127.0.0.1:15732',
           'https': '127.0.0.1:15732'}
def get_gage():
    import requests

    headers = {
        "authority": "pixabay.com",
        "accept": "application/json",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "referer": "https://pixabay.com/zh/images/search/%e6%b1%bd%e8%bd%a6/?pagi=2",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Microsoft Edge\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58",
        "x-csrftoken": "F9W96tWQfRMDkP2ZlrtfLnqhjMj1DwO7uPyfciJ030TOoDJ4Ci0KRKMKpaCKiqex",
        "x-fetch-bootstrap": "1"
    }
    cookies = {
        "csrftoken": "F9W96tWQfRMDkP2ZlrtfLnqhjMj1DwO7uPyfciJ030TOoDJ4Ci0KRKMKpaCKiqex",
        "lang": "zh",
        "anonymous_user_id": "a3853ac95d15426e8edc47e8a2c0ee94",
        "__cf_bm": "lrZ90n1ZCsYt3xo7DtCNdeP5.AVzptpVFc8LQFmR3P8-1689927766-0-AR2bJs8ibaa0J7WCapUa6EPvDjs9kGrWb3PSO9hw0faYzIfBScJZLQbNzGR0XCmqVjmhfz3xt+xMv1L8UUV5ck4=",
        "dwf_show_canva_banner_ads": "True",
        "dwf_strict_media_search": "False",
        "is_human": "1",
        "_sp_ses.aded": "*",
        "OptanonConsent": "isGpcEnabled=0&datestamp=Fri+Jul+21+2023+16%3A23%3A23+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.31.0&isIABGlobal=false&hosts=&consentId=603fee4b-7b90-4ba9-b877-0e9eef09835a&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A0%2CC0003%3A0%2CC0004%3A0&AwaitingReconsent=false",
        "_sp_id.aded": "8fb0be41-4d23-419a-a783-4a9b5591f7f2.1689927770.1.1689927804.1689927770.f1b3a17c-e599-4ecb-9883-7624b22b4662"
    }
    url = "https://pixabay.com/zh/images/search/%e6%b1%bd%e8%bd%a6/"
    url= 'https://pixabay.com/zh/images/search/%E8%BD%A6%E8%BE%86/?pagi=1'
    params = {
        "pagi": "2"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    # print(response.text)
    print(response)
    return response.json()

def save_image(name, url, PWD):
    print('save image:', url)
    # name = url.split('/')[-1]
    # 将图片以最后的数字+后缀命名，方便检查文件是否存在
    filename = PWD + name
    if os.path.isfile(filename):  # 如果文件已爬取，则不再重复爬取
        print("文件存在：", filename)
        return
    # urllib.request.urlretrieve(url, filename=path+filename)
    try:
        response = requests.get(url, proxies=proxies, headers=headers,cookies=cookies,stream=True, timeout=5)
        with open(filename, 'wb') as fd:
            fd.write(response.content)
        requests.session().close()
        return 1
    except  Exception as e:
        print(url, '保存图片失败', e)
        return 0


if __name__ =="__main__":
    response = get_gage()
    # print(response.text)
